Skip to content

fix: register lambda span - #1073

Merged
kaylareopelle merged 6 commits into
open-telemetry:mainfrom
xuan-cao-swi:register-lambda-span
Jul 23, 2024
Merged

fix: register lambda span#1073
kaylareopelle merged 6 commits into
open-telemetry:mainfrom
xuan-cao-swi:register-lambda-span

Conversation

@xuan-cao-swi

Copy link
Copy Markdown
Contributor

Description

The aws lambda span was not registered with context after start_span, which cause current_span can't detect it. User couldn't append attributes outside call_wrapped span. This PR attach the span to current context and detach it before finish.

)

trace_ctx = OpenTelemetry::Trace.context_with_span(span)
@trace_token = OpenTelemetry::Context.attach(trace_ctx)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you decide to make this an instance variable?

@xuan-cao-swi xuan-cao-swi Jul 17, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case the process fail, on the ensure part, OpenTelemetry::Context.detach(@trace_token) if @trace_token won't throw error (e.g. undefined local variable or method 'trace_token' for ...)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! I understand wanting to prevent undefined errors, and I think we can achieve that with a local variable.

One option could be to update the existing guard clause to use if defined?(trace_token).

Another option could be to add a line to define trace_token earlier on in the method, like we do for span_kind, and then use if trace_token in the guard clause:

def method
  trace_token = nil
  # ... 
ensure
  OpenTelemetry::Context.detach(trace_token) if trace_token
end

Would one of these options work for you?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will take the local variable, thanks!

@kaylareopelle kaylareopelle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

)

trace_ctx = OpenTelemetry::Trace.context_with_span(span)
trace_token = OpenTelemetry::Context.attach(trace_ctx)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xuan-cao-swi I'm curious whether you tried using tracer.in_span instead of tracer.start_span and having to explicitly manage the context.

Since you're already using OpenTelemetry::Context.with_current(parent_context) in line 43 above to propagate any incoming context into the current one, in_span should be able to continue that and as a benefit record exception too. Some examples in exising "server" or "consumer" instrumentation that deal with the same concern of propagating incoming context from external process, setting it into current process's context, and starting a span.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I agree in_span is a better choice for this instrumentation. Updated!

@kaylareopelle
kaylareopelle merged commit e7377e0 into open-telemetry:main Jul 23, 2024
This was referenced Jul 23, 2024
@xuan-cao-swi
xuan-cao-swi deleted the register-lambda-span branch January 6, 2025 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants